/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.adminplugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidSession;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  获取session列表
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidSessionListPlugin implements PluginConnector {

	private IDao bidSessionDao;
	private Logger logger = Logger.getLogger(BidSessionListPlugin.class);

	/**
	 * TODO 获取session列表插件<br>
	 * bidSession属性:按session属性获取session列表<br> 
	 * 分页设置:<br>
	 * pageSize:每页记录数;<br>
	 * pageIndex:当前页码 
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			//单独处理的特殊参数情况[暂无]
			//-------------------
			MethodParam param = setMethodParams(vo, 2, "");
			if (Validator.isEmpty(param)) {
				result.setResult("false");
				result.setMessage("查询条件为空!");
				return result;
			}
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by create_time desc ");
			param.setParams("orderBy", orderBy);
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidSessionDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidSessionDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行bidSession查询失败 ", e);
			result.setMessage("bidSession查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		MethodParam param = new MethodParam("ByCacheList", "", "", BidSession.class.getName());
		param.setResultCache(false);
		String all = String.valueOf(vo.getParams("bidSessionAll"));//获取所有活动列表
		if (Validator.isNull(all)) {
			getBidSessionParams(param, vo);
			if (Validator.isEmpty(param.getParams()) || param.getParams().size() == 0)
				return null;
		}
		return param;
	}

	//组织bidSession信息查询条件
	private void getBidSessionParams(MethodParam param, ParamsVo vo) {
		BidSession bidSession = new BidSession();
		bidSession = (BidSession) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidSession);
		String statuses = String.valueOf(vo.getParams("statuses"));
		if (Validator.isNotNull(statuses)) {
			param.setParams("statuses", statuses);
		}
		if (Validator.isNotNull(bidSession.getSessionId())) {
			param.setParams("sessionId", bidSession.getSessionId());
		}
		if (Validator.isNotNull(bidSession.getOrgEname())) {
			param.setParams("orgEname", bidSession.getOrgEname());
		}
		if (Validator.isNotNull(bidSession.getOrgCname())) {
			param.setParams("orgCname", bidSession.getOrgCname());
		}
		if (Validator.isNotNull(bidSession.getSessionName())) {
			param.setParams("sessionName", bidSession.getSessionName());
		}
		if (Validator.isNotNull(bidSession.getSessionNo())) {
			param.setParams("sessionNo", bidSession.getSessionNo());
		}
		if (Validator.isNotNull(bidSession.getSessionType())) {
			param.setParams("sessionType", bidSession.getSessionType());
		}
		if (Validator.isNotNull(bidSession.getSessionMode())) {
			param.setParams("sessionMode", bidSession.getSessionMode());
		}
		if (Validator.isNotNull(bidSession.getIfBidDyn())) {
			param.setParams("ifBidDyn", bidSession.getIfBidDyn());
		}
		if (Validator.isNotNull(bidSession.getStatus())) {
			param.setParams("status", bidSession.getStatus());
		}
		if (Validator.isNotNull(bidSession.getBailCtrl())) {
			param.setParams("bailCtrl", bidSession.getBailCtrl());
		}
		if (Validator.isNotNull(bidSession.getBailFix())) {
			param.setParams("bailFix", bidSession.getBailFix());
		}
		if (Validator.isNotNull(bidSession.getBailRate())) {
			param.setParams("bailRate", bidSession.getBailRate());
		}
		if (Validator.isNotNull(bidSession.getBailMax())) {
			param.setParams("bailMax", bidSession.getBailMax());
		}
		if (Validator.isNotNull(bidSession.getCreateTime())) {
			param.setParams("createTime", bidSession.getCreateTime());
		}
		if (Validator.isNotNull(bidSession.getModifyTime())) {
			param.setParams("modifyTime", bidSession.getModifyTime());
		}
		if (Validator.isNotNull(bidSession.getAuditTime())) {
			param.setParams("auditTime", bidSession.getAuditTime());
		}
		if (Validator.isNotNull(bidSession.getArea())) {
			param.setParams("area", bidSession.getArea());
		}
		if (Validator.isNotNull(bidSession.getCity())) {
			param.setParams("city", bidSession.getCity());
		}
		if (Validator.isNotNull(bidSession.getOrderNumber())) {
			param.setParams("orderNumber", bidSession.getOrderNumber());
		}
		if (Validator.isNotNull(bidSession.getAllowApply())) {
			param.setParams("allowApply", bidSession.getAllowApply());
		}
		if (Validator.isNotNull(bidSession.getSessionLayout())) {
			param.setParams("sessionLayout", bidSession.getSessionLayout());
		}
		if (Validator.isNotNull(bidSession.getSessionCss())) {
			param.setParams("sessionCss", bidSession.getSessionCss());
		}
		if (Validator.isNotNull(bidSession.getExceptionStatus())) {
			param.setParams("exceptionStatus", bidSession.getExceptionStatus());
		}
		if (Validator.isNotNull(bidSession.getRightCtrl())) {
			param.setParams("rightCtrl", bidSession.getRightCtrl());
		}
		if (Validator.isNotNull(bidSession.getSpecialCode())) {
			param.setParams("specialCode", bidSession.getSpecialCode());
		}
		if (Validator.isNotNull(bidSession.getSenature())) {
			param.setParams("senature", bidSession.getSenature());
		}
		if (Validator.isNotNull(bidSession.getSpare1())) {
			param.setParams("spare1", bidSession.getSpare1());
		}
		if (Validator.isNotNull(bidSession.getSpare2())) {
			param.setParams("spare2", bidSession.getSpare2());
		}
		if (Validator.isNotNull(bidSession.getSpare3())) {
			param.setParams("spare3", bidSession.getSpare3());
		}
		if (Validator.isNotNull(bidSession.getSpare4())) {
			param.setParams("spare4", bidSession.getSpare4());
		}
		if (Validator.isNotNull(bidSession.getSpare5())) {
			param.setParams("spare5", bidSession.getSpare5());
		}
		if (Validator.isNotNull(bidSession.getPicUrl())) {
			param.setParams("picUrl", bidSession.getPicUrl());
		}

		if (Validator.isNotNull(bidSession.getOpenTime())) {
			param.setParams("openTime", bidSession.getOpenTime());
		}
		if (Validator.isNotNull(bidSession.getSerialCtrl())) {
			param.setParams("serialCtrl", bidSession.getSerialCtrl());
		}
		if (Validator.isNotNull(bidSession.getDirection())) {
			param.setParams("direction", bidSession.getDirection());
		}

	}

	public void setBidSessionDao(IDao bidSessionDao) {
		this.bidSessionDao = bidSessionDao;
	}

}
